package Opera;

import BOOK.Book;
import BOOK.Booklist;

import java.util.Scanner;
/*删除的原理是将需要删除位置之后的每一个元素下标前移一位*/
public class DelOperation implements IOPeration{
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书！");
        System.out.println("请输入你要删除的图书的名字：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSized = booklist.getUsedSize();
        int index = -1;
        for (int i = 0;i < currentSized; i++) {
            Book tmp = booklist.getBook(i);//接受返回的数组下标
            if (tmp.getName().equals(name)) {
               index = i;
               break;
            }
        }
        for (int j = index;j < currentSized-1;j++){//1:08:47
            //不能等于currentsized，否则会越界
            Book book = booklist.getBook(j+1);
            booklist.setBook(j,book);
        }

        booklist.setUsedSize(currentSized - 1);
//因为删除的是对象，所以把最后一个置为null
        booklist.setBook(currentSized - 1,null);
        System.out.println("删除成功");
    }
}
